package com.fili;

import java.awt.*;
import java.util.ArrayList;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Mar 2, 2007
 * Time: 12:35:55 PM
 * Revision: 0
 */
public class BallPath
{
    public ArrayList<Point> points;
    public float len;

    public BallPath()
    {
        //default path  :)

        points = new ArrayList<Point>();
/*        points.add(new Point(0, 0));
        points.add(new Point(350, 160));
        points.add(new Point(430, 280));
        points.add(new Point(260, 310));
        points.add(new Point(120, 210));
        points.add(new Point(550, 50));*/

        points.add(new Point(0, 0));
        points.add(new Point(293, 40));
        points.add(new Point(352, 180));
        points.add(new Point(385, 226));
        points.add(new Point(400, 274));
        points.add(new Point(393, 305));
        points.add(new Point(375, 335));
        points.add(new Point(333, 348));
        points.add(new Point(286, 358));
        points.add(new Point(190, 334));
        points.add(new Point(137, 304));
        points.add(new Point(116, 278));
        points.add(new Point(105, 244));
        points.add(new Point(107, 215));
        points.add(new Point(142, 162));
        points.add(new Point(219, 143));
        points.add(new Point(361, 128));
        points.add(new Point(409, 108));
        points.add(new Point(428, 80));
        points.add(new Point(445, 32));
        points.add(new Point(471, 19));
        points.add(new Point(506, 24));
        points.add(new Point(518, 44));
        points.add(new Point(529, 143));
        points.add(new Point(532, 208));
        points.add(new Point(517, 262));
        points.add(new Point(482, 314));

        len = 0;
        for (int i = 0; i < points.size() - 1; i++)
            len += segLen(i);

    }

    private float segLen(int segNum)
    {
        Point p1, p2;
        p1 = points.get(segNum);
        p2 = points.get(segNum + 1);

        int dx = p1.x - p2.x;
        int dy = p1.y - p2.y;

        return (float) Math.sqrt(dx * dx + dy * dy);
    }

    public Point getPathPoint(float p)
    {
        int seg = 0;
        float len;

        while (true)
        {
            len = segLen(seg);
            if (p > len)
            {
                p -= len;
                seg++;
            }
            else
                return getPathPoint(p, seg);
        }
    }

    private Point getPathPoint(float p, int segNum)
    {
        Point p1, p2;
        p1 = points.get(segNum);
        p2 = points.get(segNum + 1);

        int dx = p2.x - p1.x;
        int dy = p2.y - p1.y;

        float slen = (float) Math.sqrt(dx * dx + dy * dy);
        float mul = p / slen;

        return new Point((int) (dx * mul) + p1.x, (int) (dy * mul) + p1.y);
    }

    public void paint(Graphics g)
    {
        Point p = points.get(0);

        g.setColor(Color.black);
        for (int i = 1; i < points.size(); i++)
        {
            Point p2 = points.get(i);
            g.drawLine(p.x, p.y, p2.x, p2.y);
            p = p2;
        }

        int radius = DEF.BALL_RADIUS + 10;
        g.setColor(Color.yellow);
        g.fillOval(p.x - radius, p.y - radius, radius * 2 + 1, radius * 2 + 1);
        g.setColor(Color.gray);
        g.fillOval(p.x - radius + 5, p.y - radius + 5, (radius - 5) * 2 + 1, (radius - 5) * 2 + 1);
        g.setColor(Color.black);
        g.drawOval(p.x - radius, p.y - radius, radius * 2, radius * 2);
        g.drawOval(p.x - radius + 5, p.y - radius + 5, (radius - 5) * 2, (radius - 5) * 2);
    }
}
